const {
  CloudFunction
} = require('./Cloud.js')

class get_others {
  // 这里放数据
  constructor() {

  }
  // 这个方法使用来获取插画的
  Get_Illustration() {
    const random_num = parseInt(Math.floor(Math.random() * 10 + 1))
    if (random_num == 1 || random_num == 2) {
      var chahua = 'https://kanekikeh.online/My/stu_boy.png'
    } else if (random_num == 3 || random_num == 4) {
      var chahua = 'https://kanekikeh.online/My/stu_boy1.png'
    } else if (random_num == 5 || random_num == 6) {
      var chahua = 'https://kanekikeh.online/My/stu_boy2.png'
    } else if (random_num == 7 || random_num == 8) {
      var chahua = 'https://kanekikeh.online/My/stu_girl.png'
    } else if (random_num == 9 || random_num == 10) {
      var chahua = 'https://kanekikeh.online/My/stu_girl1.png'
    }
    return chahua
  }

  // 这个方式是用来获取首页剩余天数的
  Get_RemainderTime() {
    var now = new Date();
    var year = now.getFullYear()
    var endDate = new Date(year, 11, 31, 24, 60, 60);
    var leftTime = endDate.getTime() - now.getTime(); //计算剩余的毫秒数
    if (leftTime <= 0) {
      leftTime = 0;
    }
    var days_remaining = parseInt(leftTime / 1000 / 60 / 60 / 24, 10); //计算剩余的天数
    return {
      year,
      days_remaining
    }
  }

  // 这个方法是在用户首次进入小程序使用的,用于判断用户的openid是否已经存在云数据中
  Get_UserId() {
    const UserInfo = new CloudFunction('GetUser');
    const result = UserInfo.callAsync().then(res => {
      const {
        openid,
        user_res
      } = res.result
      const db = wx.cloud.database()
      const userData = user_res.data.find(data => data._openid === openid);

      if (userData) {
        wx.setStorageSync('UserData', userData);
      } else {
        console.log('成功初始化用户信息');
        this.createNewUser(db, openid); // 调用异步函数
      }
    }).catch(err => {
      console.log('获取用户数据失败---', err)
    })
    return result
  }

  // 封装上面所需要的异步函数
  async createNewUser(db, openid) {
    const UserInfo = new CloudFunction('Add_GetUser');
    const result = UserInfo.callAsync().then(res => {
      // console.log(res)
      wx.setStorageSync('UserData', res.result)
    }).catch(err=>{
      console.log(err)
    })
 
  }

  Get_Wencui() {
    const wencuiInfo = new CloudFunction('Get_Wencui');
    const result = wencuiInfo.callAsync().then(res=>{
      const wencui = res.result.data
      return wencui
    }).catch(err=>{

    })
    return result
  }

  // 这个方法主要用于岭南文化板块
  Get_TXT(){
    const TxtInfo = new CloudFunction('GetTxt');
    const result = TxtInfo.callAsync().then(res=>{
      const txt_res = res.result.data
      return txt_res
    }).catch(err=>{
      console.log('获取数据失败---',err)
    })
    return result
  }

  // 这里封装设置头部导航
  Set_navbar() {
    const {
      top,
      height
    } = wx.getMenuButtonBoundingClientRect();
    const {
      statusBarHeight
    } = wx.getSystemInfoSync();
    const navbarHeight = (top - statusBarHeight) * 2 + height + statusBarHeight
    return navbarHeight;
  }
}



// 这里暴露方法名
module.exports = {
  get_others
}